<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <fsButton ref="button" plain size="default" :buttons="buttons" @clickHandler="buttonHandler"></fsButton>
      </div>
      <div class="right-panel">
        <ex-search ref="search" :searches="searches" @searchOk="searchHandler" default-code="noticeName" placeholder="请输入公告标题"></ex-search>
      </div>
    </el-header>
    <el-main class="nopadding">
      <el-container>
        <el-main class="nopadding">
          <ex-table id="menuTable" ref="table" :columns="columns" :url="this.$API.system.notice.list.url" :refs="this.$refs" search="search" @columnClick="columnClickHandler"></ex-table>
        </el-main>
      </el-container>
    </el-main>
  </el-container>



</template>

<script>
import saveDialog from './save'
import viewDialog from './view'
import ExTable from "@/components/exTable/index.vue";
import ExSearch from "@/components/exSearch/index.vue";
import ExSearchTree from "@/components/exSearchTree/index.vue";
import {uuid} from "@tinymce/tinymce-vue/lib/es2015/main/ts/Utils";
import BaseTemplate from "@/utils/baseTemplate";
import tool from "@/utils/tool";

export default {
  name: '公告管理',
  components: {
    viewDialog,
    ExSearchTree,
    ExSearch,
    ExTable,
    saveDialog,
  },
  data() {
    return {
      buttons: [
      {name: this.$t('button.add'), icon: "el-icon-plus", click: "add", permission: "add"},
        {name: this.$t('button.edit'), icon: "el-icon-edit", click: "edit", type: "warning", permission: "edit"},
        {name: this.$t('button.delete'), icon: "el-icon-delete", click: "del", type: "info", permission: "delete"},
        // {name: this.$t('button.enabled'), icon: "el-icon-circle-check", click: "enabled", type: "success"},
        // {name: this.$t('button.disabled'), icon: "el-icon-circle-close", click: "disabled", type: "info"},
        // {name: this.$t('button.detail'), icon: "el-icon-document", click: "show"},
        // {name: this.$t('button.export'), icon: "el-icon-download", click: "export"},
      ],
      searches: [
        { type: "input", name: "公告标题", code: "noticeName", placeholder: "请输入公告标题", maxlength: 30, showLimit: true},
      ],
      columns: [
      {code: "操作", name: "操作", width: "100",
      click:[{label:"",icon:"el-icon-edit-pen",event:'detailedit',permission: "edit"},
      {label:"",icon:"el-icon-delete",event:'detaildel',permission: "delete"}]},
        {code: "noticeName", name: "公告标题", width: "",click:"show1"},
        {code: "isEnabled", name: "是否可用", width: "100", sortable: true, formatter: "DICT", key: "IS_ENABLED"},
        {code: "createUser", name: "创建人", width: "100", formatter: "DICT", key: "SysUser"},
        {code: "createTime", name: "创建时间", width: "175", sortable: true, formatter: "DATETIME"},
      ],
      parentLevelCode: null,
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    buttonHandler(index, name) {
      this.$options.methods[name](this, index);
    },
    async searchTreeHandler(data) {
      this.parentLevelCode = data.levelCode;
      let queryParams = {pathCode: this.parentLevelCode}
      await this.$refs.table.refresh(queryParams);
    },
    async searchHandler() {
      let queryParams = {pathCode: this.parentLevelCode}
      await this.$refs.table.refresh(queryParams);
    },
    columnClickHandler(name,data){
      this.$options.methods[name](this, data);
    },
    //添加
    add(_this) {
      let title="新增公告";
      let fromRouteId = _this.$TOOL.crypto.MD5(_this.$route.path);//当前页面的路由地址
      let key = _this.$TOOL.crypto.MD5(uuid("notice"));//可以做为key的关键字段或内容
      let routeParams = {
				id: key,
				title: title,
			};
      _this.$store.dispatch("addRouteParams", routeParams);
      _this.$router.push({
				name: "noticeadd",
				params: {fromRouteId: fromRouteId, key: key}
			});
     // _this.$nextTick(() => {
      //  _this.$refs.saveDialog.open(data);
      //})
    },
    //编辑
    edit(_this) {
      let row = _this.$refs.table.selectionOne();
      if (!row) {
        return;
      }
      let title="编辑公告";
      let fromRouteId = _this.$TOOL.crypto.MD5(_this.$route.path);//当前页面的路由地址
			let key = '';
			if(row){
				key = _this.$TOOL.crypto.MD5(row.code);//可以做为key的关键字段或内容
			}else{
				key = _this.$TOOL.crypto.MD5(uuid("s"));//可以做为key的关键字段或内容
			}
      //设置搜索参数
			let routeParams = {
				id: key,
				title: title,
				fixedParams:{
          id:row.id,
				},//固定搜索参数，打开页面后，用户通过搜索界面的清空按钮无法清除
			};
      _this.$store.dispatch("addRouteParams", routeParams);
      _this.$router.push({
				name: "noticeadd",
				params: {fromRouteId: fromRouteId, key: key}
			});
    },

 //单个编辑
 detailedit(_this,row) {
      //设置搜索参数
      let title="编辑公告";
      let fromRouteId = _this.$TOOL.crypto.MD5(_this.$route.path);//当前页面的路由地址
			let key = '';
			if(row){
				key = _this.$TOOL.crypto.MD5(row.code);//可以做为key的关键字段或内容
			}else{
				key = _this.$TOOL.crypto.MD5(uuid("s"));//可以做为key的关键字段或内容
			}
      //设置搜索参数
			let routeParams = {
				id: key,
				title: title,
				fixedParams:{
          id:row.id,
				},//固定搜索参数，打开页面后，用户通过搜索界面的清空按钮无法清除
			};
      _this.$store.dispatch("addRouteParams", routeParams);
      _this.$router.push({
				name: "noticeadd",
				params: {fromRouteId: fromRouteId, key: key}
			});

     // _this.$nextTick(() => {
      //  _this.$refs.saveDialog.open(row);
      //})
    },
    //查看
    show(_this) {
      let row = _this.$refs.table.selectionOne();
      if (!row) {
        return;
      }
      let title="查看公告";
      let fromRouteId = _this.$TOOL.crypto.MD5(_this.$route.path);//当前页面的路由地址
			let key = '';
			if(row){
				key = _this.$TOOL.crypto.MD5(row.code);//可以做为key的关键字段或内容
			}else{
				key = _this.$TOOL.crypto.MD5(uuid("s"));//可以做为key的关键字段或内容
			}
      //设置搜索参数
			let routeParams = {
				id: key,
				title: title,
				fixedParams:{
          id:row.id,
				},//固定搜索参数，打开页面后，用户通过搜索界面的清空按钮无法清除
			};
      _this.$store.dispatch("addRouteParams", routeParams);
    //  _this.$nextTick(() => {
      //  _this.$refs.viewDialog.open(row);
      //})
      _this.$router.push({
				name: "noticeview",
				params: {fromRouteId: fromRouteId, key: key}
			});
    },
    //查看
    show1(_this,row) {
     // _this.$nextTick(() => {
     //   _this.$refs.viewDialog.open(row);
     // })
     if (!row) {
        return;
      }
      let title="查看公告";
      let fromRouteId = _this.$TOOL.crypto.MD5(_this.$route.path);//当前页面的路由地址
			let key = '';
			if(row){
				key = _this.$TOOL.crypto.MD5(row.code);//可以做为key的关键字段或内容
			}else{
				key = _this.$TOOL.crypto.MD5(uuid("s"));//可以做为key的关键字段或内容
			}
      //设置搜索参数
			let routeParams = {
				id: key,
				title: title,
				fixedParams:{
          id:row.id,
				},//固定搜索参数，打开页面后，用户通过搜索界面的清空按钮无法清除
			};
      _this.$store.dispatch("addRouteParams", routeParams);
    //  _this.$nextTick(() => {
      //  _this.$refs.viewDialog.open(row);
      //})
      _this.$router.push({
				name: "noticeview",
				params: {fromRouteId: fromRouteId, key: key}
			});
    },
    //导出
    async export(_this,index) {
      let attachParams = {};
      let exportApi = _this.$API.system.notice.export;
      let exTableRef = _this.$refs.table;
      let exportBtnRef = _this.$refs.button;
      let exportBtnIndex = index;
      await BaseTemplate.export(_this, exportApi, attachParams, exportBtnRef, exportBtnIndex, exTableRef);
    },
    //批量删除
    del(_this, index) {
      let selections = _this.$refs.table.selections();
      if (selections.length === 0) {
        _this.$message.warning(_this.$t("common.please_select_data"));
        return;
      }
      _this.$confirm(`确定删除选中的 ${selections.length} 项吗？`, _this.$t('common.warn'), {
        type: 'warning'
      }).then(async () => {
        _this.$refs.button.loading(index, true);
        var delarray = [];
        for (let index = 0; index < selections.length; index++) {
          const element = selections[index];
          delarray.push(element.id);
        }
        var res = await _this.$API.system.notice.deleteByIds.post(delarray);
		  if (res.code === 2000) {
			  _this.$message.success(_this.$t("code."+res.code));
			  _this.searchHandler();
			  _this.$store.dispatch("commit",selections[0].entityName);
		  }
        _this.$refs.button.loading(index, false);
      }).catch(() => {
        _this.$refs.button.loading(index, false);
      });
    },
    //单个删除
   detaildel(_this, row) {
      let selections = [];
      selections.push(row);
      _this.$confirm(`确定删除选中的 ${selections.length} 项吗？`, _this.$t('common.warn'), {
        type: 'warning'
      }).then(async () => {
        var delarray = [];
        for (let index = 0; index < selections.length; index++) {
          const element = selections[index];
          delarray.push(element.id);
        }
        var res = await _this.$API.system.notice.deleteByIds.post(delarray);
		  if (res.code === 2000) {
			  _this.$message.success(_this.$t("code."+res.code));
			  _this.searchHandler();
			  _this.$store.dispatch("commit",selections[0].entityName);
		  }
      }).catch(() => {
      });
    },
    //批量启用
    enabled(_this, index) {
      let selections = _this.$refs.table.selections();
      if (selections.length === 0) {
        _this.$message.warning(_this.$t("common.please_select_data"));
        return;
      }
      _this.$confirm(`确定启用选中的 ${selections.length} 项吗？`, _this.$t('common.warn'), {
        type: 'warning'
      }).then(async () => {
        _this.$refs.button.loading(index, true);
        var delarray = [];
        for (let index = 0; index < selections.length; index++) {
          const element = selections[index];
          delarray.push(element.id);
        }
        var res = await _this.$API.system.notice.enabled.post(delarray);
        if (res.code === 2000) {
          _this.$message.success(_this.$t("code."+res.code));
          _this.searchHandler();
		  _this.$store.dispatch("commit",selections[0].entityName);
        }
        _this.$refs.button.loading(index, false);
      }).catch(() => {
        _this.$refs.button.loading(index, false);
      });
    },
    //批量禁用
    disabled(_this, index) {
      let selections = _this.$refs.table.selections();
      if (selections.length === 0) {
        _this.$message.warning(_this.$t("common.please_select_data"));
        return;
      }
      _this.$confirm(`确定禁用选中的 ${selections.length} 项吗？`, _this.$t('common.warn'), {
        type: 'warning'
      }).then(async () => {
        _this.$refs.button.loading(index, true);
        var delarray = [];
        for (let index = 0; index < selections.length; index++) {
          const element = selections[index];
          delarray.push(element.id);
        }
        var res = await _this.$API.system.notice.disabled.post(delarray);
        if (res.code === 2000) {
          _this.$message.success(_this.$t("code."+res.code));
          _this.searchHandler();
		  _this.$store.dispatch("commit",selections[0].entityName);
        }
        _this.$refs.button.loading(index, false);
      }).catch(() => {
        _this.$refs.button.loading(index, false);
      });
    },
  }
}
</script>
